package com.tbit.uqbike.enums;

/**
 * 指令解析枚举
 *
 * @Author:LHX
 * @DATE: 2023/9/4
 */
public enum OrderAnalyze {


    //contro
    CONTROL_TYPE_LOCK("1", "远程控制-上锁"),
    CONTROL_TYPE_UNSTART("2", "撤防"),
    CONTROL_TYPE_RESTART("3", "远程控制-重启"),
    CONTROL_TYPE_LOCATION("8", "定位"),
    CONTROL_TYPE_FIND("9", "寻车"),
    CONTROL_TYPE_UNLOCK("11", "远程控制-开锁"),
    CONTROL_TYPE_COERCE_LOCK("12", "远程控制-强制关锁"),
    CONTROL_TYPE_BATTERYDE("15", "远程控制-打开电池锁"),
    CONTROL_TYPE_BATTERYRIDE("16", "远程控制-关闭电池锁"),
    CONTROL_TYPE_OPEN_HELMET("22", "远程控制-打开头盔锁"),
    CONTROL_TYPE_CLOSE_HELMET("23", "远程控制-关闭头盔锁"),
    CONTROL_TYPE_PARKRIDE("48", "远程控制-临时停车"),
    CONTROL_TYPE_CONTINUERIDE("49", "远程控制-继续骑行"),
    CONTROL_POINT("51", "远程控制-上传融合定位包"),
    CONTROL_TYPE_VERT("55", "远程查询垂直停车角度"),
    CONTROL_AICAMERARET("57", "远程控制-查询摄像头"),
    CONTROL_RFIDREADSTATUS("61", "远程控制-查询RFID"),
    CONTROL_TYPE_COERCE_UNLOCK("71", "远程控制-强制开锁"),
    MACHINE_CONTROL_CLOSE_HRESTE("78", "氢气复位"),
    CONTROL_RETURNBIKE("84", "远程控制-预还车指令"),
    CONTROL_TYPE_PHOTO("takePic", "通知设备拍照"),

    //voice
    TER_VOICE_1("1", "语音播报-欢迎使用共享电单车"),
    TER_VOICE_2("2", "语音播报-没电了，请换一辆车"),
    TER_VOICE_3("3", "语音播报-骑出运营区，请尽快返回"),
    TER_VOICE_4("4", "语音播报-报警音，车辆未解锁，请扫码骑行"),
    TER_VOICE_5("5", "语音播报-HI，主人，我在这辆哦"),
    CONTROL_TYPE_VOICE_NOT_IN_PARKPOINT("9", "不在站点"),
    TER_VOICE_12("12", "语音播报-距离服务器过远，车辆即将断电"),
    TER_VOICE_20("20", "语音播报-出围栏预警"),
    CONTROL_TYPE_VOICE_SXT("39", "摄像头语音"),
    CONTROL_TYPE_VOICE_HELMET("38", "头盔锁语音指令"),
    CONTROL_TYPE_VOICE_FRID("40", "rfid语音指令"),
    CONTROL_TYPE_VOICE_DDYY("53", "在骑车辆低电语音"),
    CONTROL_TYPE_VOICE_DDDD("54", "在骑车辆低电断电"),

    //get
    CONTROL_TYPE_DEVICESTATUS("DEVICESTATUS;", "查询终端状态"),
    CONTROL_TYPE_DEVICESTATUSEX("DEVICESTATUSEX;", "查询终端状态扩展"),
    CONTROL_TYPE_RFIDREADSTATUS("RFIDREADSTATUS;", "rfid状态"),
    CONTROL_TYPE_AICAMERARET("AICAMERARET;", "摄像头状态"),
    CONTROL_TYPE_VEHHEADINGANGLE("VEHHEADINGANGLE;", "偏向角状态"),

    //set
    CONTROL_TYPE_RFIDPOWER("RFIDPOWER", "rfid断电"),
    HELMET("DFTBLEHELMETMAC", "设置智能头盔锁mac"),
    MANNED_BOUND("DFTBLEMANNEDMAC", "载人设备");


    //contro
    public static final OrderAnalyze[] contro = {
            CONTROL_TYPE_LOCK,
            CONTROL_TYPE_UNSTART,
            CONTROL_TYPE_RESTART,
            CONTROL_TYPE_LOCATION,
            CONTROL_TYPE_FIND,
            CONTROL_TYPE_UNLOCK,
            CONTROL_TYPE_COERCE_LOCK,
            CONTROL_TYPE_BATTERYDE,
            CONTROL_TYPE_BATTERYRIDE,
            CONTROL_TYPE_OPEN_HELMET,
            CONTROL_TYPE_CLOSE_HELMET,
            CONTROL_TYPE_PARKRIDE,
            CONTROL_TYPE_CONTINUERIDE,
            CONTROL_POINT,
            CONTROL_TYPE_VERT,
            CONTROL_AICAMERARET,
            CONTROL_RFIDREADSTATUS,
            CONTROL_TYPE_COERCE_UNLOCK,
            MACHINE_CONTROL_CLOSE_HRESTE,
            CONTROL_RETURNBIKE,
            CONTROL_TYPE_PHOTO
    };
    //voice
    public static final OrderAnalyze[] voice = {
            TER_VOICE_1,
            TER_VOICE_2,
            TER_VOICE_3,
            TER_VOICE_4,
            TER_VOICE_5,
            CONTROL_TYPE_VOICE_NOT_IN_PARKPOINT,
            TER_VOICE_12,
            TER_VOICE_20,
            CONTROL_TYPE_VOICE_SXT,
            CONTROL_TYPE_VOICE_HELMET,
            CONTROL_TYPE_VOICE_FRID,
            CONTROL_TYPE_VOICE_DDYY,
            CONTROL_TYPE_VOICE_DDDD
    };
    //get
    public static final OrderAnalyze[] get = {
            CONTROL_TYPE_DEVICESTATUS,
            CONTROL_TYPE_DEVICESTATUSEX,
            CONTROL_TYPE_RFIDREADSTATUS,
            CONTROL_TYPE_AICAMERARET,
            CONTROL_TYPE_VEHHEADINGANGLE
    };
    //set
    public static final OrderAnalyze[] set = {
            CONTROL_TYPE_RFIDPOWER,
            HELMET,
            MANNED_BOUND
    };
    /**
     * 指令内容
     */
    private String name;
    /**
     * 指令解析
     */
    private String value;

    OrderAnalyze(String name, String value) {
        this.name = name;
        this.value = value;
    }

    /**
     * 解析指令
     *
     * @return 指令解析结果
     */
    public static String analyze(String orderType, String orderContent) {
        switch (orderType) {
            //远程控制
            case "contro":
            //远程控制（携带TLV）
            case "controlTlv":
                for (OrderAnalyze orderAnalyze : OrderAnalyze.contro) {
                    if (orderAnalyze.getName().equals(orderContent)) {
                        return orderAnalyze.getValue();
                    }
                }
                break;
            case "voice":
                for (OrderAnalyze orderAnalyze : OrderAnalyze.voice) {
                    if (orderAnalyze.getName().equals(orderContent)) {
                        return orderAnalyze.getValue();
                    }
                }
                break;
            case "get":
                StringBuilder content = getContent(orderContent, OrderAnalyze.get);
                //如果content为空字符串返回null
                if (!"".contentEquals(content)) {
                    return content.toString();
                }
                break;
            case "set":
                StringBuilder contentSet = getContent(orderContent, OrderAnalyze.set);
                //如果content为空字符串返回null
                if (!"".contentEquals(contentSet)) {
                    return contentSet.toString();
                }
                break;
            default:
                return null;
        }
        return null;
    }

    private static StringBuilder getContent(String orderContent, OrderAnalyze[] analyzes) {
        StringBuilder content = new StringBuilder();
        String[] split = orderContent.split(";");

        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            for (OrderAnalyze orderAnalyze : analyzes) {
                if (orderAnalyze.getName().contains(s) || s.contains(orderAnalyze.getName())) {
                    content.append(orderAnalyze.getValue());
                    if (i < split.length - 1) {
                        content.append(";");
                    }
                    break;
                }
            }

        }
        return content;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }


}
